package com.cangshi.controller;

import com.alibaba.fastjson.JSON;
import com.cangshi.dao.UserMapper;
import com.cangshi.entity.Bill;
import com.cangshi.entity.JsonObject;
import com.cangshi.entity.User;
import com.cangshi.entity.UserBaseInfo;
import com.cangshi.service.BillService;
import com.cangshi.service.UserService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by Eoly on 2016/12/22.
 */

@Controller
public class BillController {

    private static final Logger LOGGER = Logger.getLogger(BillController.class);

    @Autowired
    BillService billService;

    @Autowired
    UserService userService;


    /**
     * 管理员余额充值通道
     *
     * @param userId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/user/recharge-balance", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
    public JsonObject userRecharge(HttpServletRequest request, @RequestParam(required = false) Integer userId, @RequestParam(required = false) Double value, @RequestParam(required = false) String info, @RequestParam(required = false) String userLoginPsw) {
        JsonObject jsonObject = new JsonObject();

        //获得用户
        UserBaseInfo userLogin = (UserBaseInfo) request.getAttribute("user");

        //获取用户实体以获取支付密码
        User user = userService.getUserById(userLogin.getUserId());
        String loginPsw = user.getUserLoginPsw();

        //验证完整性
        if(value==null||userId==null||userLoginPsw==null){
            jsonObject.setError(705);
            jsonObject.setMes("充值信息不完整，请检查后重新填写");
        }else if(value<=0){
            jsonObject.setError(705);
            jsonObject.setMes("充值金额应大于0");
        }else if(!userLoginPsw.equals(loginPsw)){
            jsonObject.setError(708);
            jsonObject.setMes("密码输入错误");
        }
        else if (userLogin.getUserPower() == 3) {//权限审核
            //实例化账单
            Bill bill = new Bill();

            //填充账单数据
            bill.setUserId(userId);
            bill.setBillChangeType(1);
            bill.setBillChangeValue(value);
            bill.setBillOrigin("管理员充值");

            if(info!=null)
                bill.setBillInfo(info);
            else
                bill.setBillInfo("没有备注信息");

            //添加账单操作并获取结果
            Integer result = billService.addBill(bill);
            jsonObject.setError(result==1?0:result);
            jsonObject.setMes(result==1?"充值成功":"充值失败");

            if(result==702){
                jsonObject.setMes("被充值的用户id"+userId+"没有找到，请检查");
            }

        } else {
            jsonObject.setError(701);
            jsonObject.setMes("您没有权限充值");
        }

        return jsonObject;
    }

    /**
     * 获得用户余额
     *
     * @param request 用户请求
     * @return 结果json
     */
    @ResponseBody
    @RequestMapping(value = "user/show-balance", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
    public JsonObject showBalance(HttpServletRequest request) {

        JsonObject<User> jsonObject = new JsonObject<User>();

        //获得用户
        UserBaseInfo userLogin = (UserBaseInfo) request.getAttribute("user");
        User user = userService.getUserById(userLogin.getUserId());

        if (user != null) {


            //封装用户余额信息
            User resultUser = new User();
            resultUser.setUserId(userLogin.getUserId());
            resultUser.setUserBalance(user.getUserBalance());

            jsonObject.setError(0);
            jsonObject.setData(resultUser);

        } else {
            jsonObject.setError(310);
            jsonObject.setMes("获取失败");
        }


        return jsonObject;
    }

    /**
     * 获得用户账单
     *
     * @param request 用户请求
     * @return 结果json
     */
    @ResponseBody
    @RequestMapping(value = "/user/show-bills", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
    public JsonObject showBills(HttpServletRequest request) {

        JsonObject<Bill> jsonObject = new JsonObject<Bill>();

        //获得用户
        UserBaseInfo userLogin = (UserBaseInfo) request.getAttribute("user");

        List<Bill> resultBills = billService.findByUserId(userLogin.getUserId());

        if (resultBills!=null) {
            if (!resultBills.isEmpty()) {
                jsonObject.setDataList(resultBills);
                jsonObject.setError(0);
                jsonObject.setMes("获取成功，共获取" + resultBills.size() + "条账单信息");
            } else {
                jsonObject.setError(0);
                jsonObject.setMes("该用户的账单信息为空");
            }
        }else {
            jsonObject.setError(707);
            jsonObject.setMes("获取账单信息失败");
        }

        return jsonObject;
    }
}
